<?php
class Index extends CI_Controller {
	function __construct() {
		parent::__construct ();
		session_start();
		if (! isset ( $_SESSION ["admin_id"] ) || $_SESSION ["admin_id"] == "") {
			header ( "Location:" . site_url ( 'admin/login' ) );
		}
		$this->load->helper ( 'url' );
	}
	function index() {
		
		$this->load->view ( 'admin/v_index' );
	}
	// 首页商品导航分类
	function topnav() {
		// 获取商品分类导航栏
		$sql = "SELECT id,`name`,f_id FROM `tb_nav` where f_id = 0 ORDER BY `order`";
		$nav = $this->m_tbk->tbk_query ( $sql ); // 一级导航
		foreach ( $nav as $key => $v ) { // 二级导航
			$sql = 'SELECT id,`name`,f_id FROM `tb_nav` where f_id = ' . $v->id . ' ORDER BY `order`';
			if (! $this->m_tbk->tbk_query ( $sql )) {
				$nav [$key]->c_nav = array();
			} else
				$nav [$key]->c_nav = $this->m_tbk->tbk_query ( $sql );
		}
		$lang ["nav"] = $nav;
		$this->load->view ( 'admin/v_topnav', $lang );
	}
	function topnavpost() {
		try {
			for($i = 0; isset ( $_POST ["f_$i"] ); $i ++) {
				if (isset ( $_POST ["id_$i"] )) { // 更新节点
					$name = trim ( $_POST ["f_$i"] ); // 获取新的节点名称
					$f_id = $_POST ["id_$i"]; // 获取更新节点id
					if (! $this->update_cc ( $name, 0, "c" )) { // 判断新的节点名称时候存在
						$this->update_cc ( $name, $f_id, "update" );
					}
					for($j = 0; isset ( $_POST ["c_" . $i . "_" . $j] ); $j ++) { // 子节点
						$name = trim ( $_POST ["c_" . $i . "_" . $j] );
						if (isset ( $_POST ["id_" . $i . "_" . $j] )) {
							$id = trim ( $_POST ["id_" . $i . "_" . $j] );
							if ($name == "") {
								$this->update_cc ( $name, $id, "del" );
							}
							if (! $this->update_cc ( $name, $f_id, "c" )) { // 判断新的节点名称时候存在
								$this->update_cc ( $name, $id, "update" );
							}
						} else {
							// 添加新的节点
							if ($name == "")
								continue;
							if (! $this->update_cc ( $name, $f_id, "c" )) { // 判断新的节点名称时候存在
								$this->update_cc ( $name, $f_id, "add" );
							}
						}
					}
				} else { // 添加新的节点
				}
			}
		} catch ( Exception $e ) {
		}
		header ( "Location:" . site_url ( "admin/index/topnav" ) );
	}
	/**
	 * 查询，判断，更新,删除
	 *
	 * @param unknown_type $name        	
	 * @param unknown_type $f        	
	 */
	private function update_cc($name, $f, $fun) {
		switch ($fun) {
			case "c" : // 判断是否存在该字段
				if ($f == 0)
					$sql = "SELECT id,`name`,f_id FROM `tb_nav` where f_id = 0 and `name`= '$name'";
				else
					$sql = "SELECT id,`name`,f_id FROM `tb_nav` where f_id = $f and `name`= '$name'";
				return $this->m_tbk->tbk_query ( $sql );
			case "update" :
				$sql = "update `tb_nav` set `name`= '$name' where id=$f ";
				return $this->m_tbk->querys ( $sql );
			case "add" :
				$data ["name"] = $name;
				$data ["f_id"] = $f;
				return $this->m_tbk->add_tjb ( 'nav', $data );
			case "del" :
				$sql = "DELETE FROM `tb_nav` WHERE (`id`=$f)";
				return $this->m_tbk->querys ( $sql );
		}
		return false;
	}
	// 首页商品关键字修改
	function topkey() {
		$where ["id"] = 1;
		$config = $this->m_tbk->getOne ( "config", $where );
		$lang ['config'] = $config;
		$this->load->view ( 'admin/v_topkey', $lang );
	}
	// 修改首页商品关键字
	function updatekey() {
		$date ["hot_key"] = trim ( $_POST ["hot_key"] );
		$where ["id"] = "1";
		if ($this->m_tbk->update ( "config", $date, $where )) {
			$this->topkey ();
		} else {
			echo "更新失败！";
		}
	}
	function top() {
		$this->load->view ( 'admin/v_top' );
	}
	function left() {
		$this->load->view ( 'admin/v_left' );
	}
	function right() {
		$this->load->view ( 'admin/v_right' );
	}
	function right_top() {
		$this->load->view ( 'admin/v_right_top' );
	}
}